#ifndef  _CAN_USE_H
#define _CAN_USE_H

#include <cmsis_os2.h>
#include "bsp_can.h"

typedef enum {
  CAN_M3508_M1_ID = 0x201, /* 1 */
  CAN_M3508_M2_ID = 0x202, /* 2 */
  CAN_M3508_M3_ID = 0x203, /* 3 */
  CAN_M3508_M4_ID = 0x204, /* 4 */

	CAN_G6020_AgvM1 =0x205,
	CAN_G6020_AgvM2 =0x206,
	CAN_G6020_AgvM3 =0x207,
	CAN_G6020_AgvM4 =0x208,
	  
	CAN_VESC5065_M1 =0x211,
	CAN_VESC5065_M2 =0x212,
	CAN_VESC5065_M3 =0x213,
	CAN_VSEC5065_M4 =0x214,
	
	
} CAN_MotorId_t;


typedef enum {
  CAN_MOTOR_CHASSIS6020 = 0,
  CAN_MOTOR_CHASSIS5065,
  CAN_MOTOR_GROUT_NUM,
} CAN_MotorGroup_e;

typedef union {
  float as_array[4];
  struct {
    float m1;
    float m2;
    float m3;
    float m4;
  } named;
} CAN_DJIOutput_t;


typedef union {
  int erpm[4];
  struct {
    int m1;
    int m2;
    int m3;
    int m4;
  } named;
} CAN_VescOutput_t;

typedef union{
		int as_array;
		struct{
		int8_t byte1;
		int8_t byte2;
		int8_t byte3;
		int8_t byte4;
		}byte;
}Vesc_ByteGet;

typedef struct {
  CAN_DJIOutput_t chassis6020;
  CAN_VescOutput_t chassis5065;
} CAN_Output_t;


typedef struct {
  CAN_RxHeaderTypeDef rx_header;
  uint8_t rx_data[8];
} CAN_RawRx_t;

typedef struct {
  CAN_TxHeaderTypeDef tx_header;
  uint8_t tx_data[8];
} CAN_RawTx_t;

typedef struct {
  BSP_CAN_t chassis6020;
  BSP_CAN_t chassis5065;
} CAN_Params_t;


/* 电机反馈信息 */
typedef struct {
  float rotor_angle;
  float rotor_speed;
  float torque_current;
  float temp;
} CAN_MotorFeedback_t;


typedef union {
  CAN_MotorFeedback_t as_array[4];
  struct {
    CAN_MotorFeedback_t m1;
    CAN_MotorFeedback_t m2;
    CAN_MotorFeedback_t m3;
    CAN_MotorFeedback_t m4;
  } named;
} CAN_ChassisMotor_t;


typedef struct {
  CAN_ChassisMotor_t chassis6020;
  CAN_ChassisMotor_t chassis5065;
} CAN_Motor_t;

typedef struct {
  uint32_t recive_flag;

  CAN_Motor_t motor;
  const CAN_Params_t *param;
  struct {
    uint32_t chassis;
  } mailbox;
  osMessageQueueId_t msgq_raw;
} CAN_t;

int8_t CAN_Init(CAN_t *can,const CAN_Params_t *param);

int8_t CAN_StoreMsg(CAN_t *can,CAN_RawRx_t *can_rx);

bool_t CAN_CheckFlag(CAN_t *can,uint32_t flag);

int8_t CAN_ClearFlag(CAN_t *can,uint32_t flag);

int8_t CAN_DJIMotor_Control(CAN_MotorGroup_e group,CAN_Output_t *output,CAN_t *can);

int8_t CAN_VESC_Control(int id,CAN_MotorGroup_e group, CAN_Output_t *output,CAN_t *can);




#endif
